package kr.or.common.util;

import kr.or.common.security.ibatis.IbatisUserDetail;

import org.springframework.security.core.context.SecurityContextHolder;
import kr.or.common.security.ibatis.*;
import kr.or.javacafe.member.domain.User;


public class RoleUtil {

	public static User getUserInfo() {
		if(SecurityContextHolder.getContext().getAuthentication() == null) return null;
		
		Object princial = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (princial instanceof String && princial.equals("anonymousUser")) {
			return null;
		}
		else 
		{
			return ((IbatisUserDetail) princial).getUser();
		}
	}
	
	
	public static String getRoleByBoardRead(String strBoardRoleType, String strReadWrite, String strRoleGrpId) {
		// G0 : 로그인전 권한(anonymous)
		// G1 : 일반사용자 권한(user)
		// G2 : 팀리더 권한(manager)
		// G3 : 운영진 권한(admin)
		// G4 : 관리자 권한(god)
		
		/*
		--------------------------------------------------
		TYPE_A(관리자관리 타입) - 관리자 RW, 운영진 R, 나머지 R
		--------------------------------------------------
		@  G0 G1 G2 G3 G4
		R  O  O  O  O  O
		W  X  X  X  X  O
		*/
		if (strBoardRoleType.equals("TYPE_A"))
		{
			if (strRoleGrpId.equals("G0")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G1")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G2")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G3")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G4")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
		}
		
		/*
		--------------------------------------------------
		TYPE_B(운영진관리 타입) - 관리자 RW, 운영진 RW, 나머지 R
		--------------------------------------------------
		@  G0 G1 G2 G3 G4
		R  O  O  O  O  O
		W  X  X  X  O  O
		*/
		else if (strBoardRoleType.equals("TYPE_B"))
		{
			if (strRoleGrpId.equals("G0")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G1")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G2")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G3")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
			else if (strRoleGrpId.equals("G4")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}			
		}

		/*
		--------------------------------------------------
		TYPE_C(비밀게시판 타입) - 관리자 RW, 운영진 RW
		--------------------------------------------------
		@  G0 G1 G2 G3 G4
		R  X  X  X  O  O
		W  X  X  X  O  O
		*/
		if (strBoardRoleType.equals("TYPE_C"))
		{
			if (strRoleGrpId.equals("G0")) {
				if (strReadWrite.equals("R")) return "";
				else return "";
			}
			else if (strRoleGrpId.equals("G1")) {
				if (strReadWrite.equals("R")) return "";
				else return "";
			}
			else if (strRoleGrpId.equals("G2")) {
				if (strReadWrite.equals("R")) return "";
				else return "";
			}
			else if (strRoleGrpId.equals("G3")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
			else if (strRoleGrpId.equals("G4")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}			
		}
		
		/*
		--------------------------------------------------
		TYPE_D(일반게시판 타입) - 관리자 RW, 운영진 RW, 나머지 RW
		--------------------------------------------------
		@  G0 G1 G2 G3 G4
		R  O  O  O  O  O
		W  X  O  O  O  O
		*/
		else
		{
			if (strRoleGrpId.equals("G0")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "";
			}
			else if (strRoleGrpId.equals("G1")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
			else if (strRoleGrpId.equals("G2")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
			else if (strRoleGrpId.equals("G3")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}
			else if (strRoleGrpId.equals("G4")) {
				if (strReadWrite.equals("R")) return "OK";
				else return "OK";
			}			
		}
		
		return "";
	}
	
	
	
}
